package com.foreveredu.propms.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.foreveredu.propms.domain.Department;
import com.foreveredu.propms.persist.IDepartmentDao;
import com.foreveredu.propms.domain.Building;
import com.foreveredu.propms.domain.BuildingType;
import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.domain.HouseSource;
import com.foreveredu.propms.domain.HouseType;
import com.foreveredu.propms.domain.Message;
import com.foreveredu.propms.domain.News;
import com.foreveredu.propms.domain.StrongNews;
import com.foreveredu.propms.domain.Owner;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.domain.Role;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.domain.User;
import com.foreveredu.propms.persist.IBuildingDao;
import com.foreveredu.propms.persist.IBuildingTypeDao;
import com.foreveredu.propms.persist.IDistrictDao;
import com.foreveredu.propms.persist.IHouseDao;
import com.foreveredu.propms.persist.IHouseSourceDao;
import com.foreveredu.propms.persist.IHouseTypeDao;
import com.foreveredu.propms.persist.IMessageDao;
import com.foreveredu.propms.persist.INewsDao;
import com.foreveredu.propms.persist.IOwnerDao;
import com.foreveredu.propms.persist.IStrongNewsDao;
import com.foreveredu.propms.persist.ISubDistrictDao;
import com.foreveredu.propms.persist.IUserDao;
import com.foreveredu.propms.persist.IRoleDao;
import com.foreveredu.propms.service.IManagementService;

@Transactional
public class ManagementService implements IManagementService {

	private IRoleDao roleDao = null;
	private IUserDao userDao = null;
	private IDepartmentDao deptDao = null;
	private IDistrictDao districtDao = null;
	private ISubDistrictDao subDistrictDao = null;
	private IBuildingDao buildingDao = null;
	private IHouseDao houseDao = null;
	private IOwnerDao ownerDao = null;
	private INewsDao newsDao = null;
	private IMessageDao messageDao = null;
	private IBuildingTypeDao buildingTypeDao = null;
	private IHouseTypeDao houseTypeDao = null;
	private IHouseSourceDao houseSourceDao = null;
	private IStrongNewsDao strongNewsDao = null;

	protected MessageSourceAccessor messages = SpringSecurityMessageSource
			.getAccessor();

	public IHouseTypeDao getHouseTypeDao() {
		return houseTypeDao;
	}

	public void setHouseTypeDao(IHouseTypeDao houseTypeDao) {
		this.houseTypeDao = houseTypeDao;
	}

	public IHouseSourceDao getHouseSourceDao() {
		return houseSourceDao;
	}

	public void setHouseSourceDao(IHouseSourceDao houseSourceDao) {
		this.houseSourceDao = houseSourceDao;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IDepartmentDao getDeptDao() {
		return deptDao;
	}

	public void setDeptDao(IDepartmentDao deptDao) {
		this.deptDao = deptDao;
	}

	public IDistrictDao getDistrictDao() {
		return districtDao;
	}

	public void setDistrictDao(IDistrictDao districtDao) {
		this.districtDao = districtDao;
	}

	public ISubDistrictDao getSubDistrictDao() {
		return subDistrictDao;
	}

	public void setSubDistrictDao(ISubDistrictDao subDistrictDao) {
		this.subDistrictDao = subDistrictDao;
	}

	public IBuildingDao getBuildingDao() {
		return buildingDao;
	}

	public void setBuildingDao(IBuildingDao buildingDao) {
		this.buildingDao = buildingDao;
	}

	public IBuildingTypeDao getBuildingTypeDao() {
		return buildingTypeDao;
	}

	public IHouseDao getHouseDao() {
		return houseDao;
	}

	public void setBuildingTypeDao(IBuildingTypeDao buildingTypeDao) {
		this.buildingTypeDao = buildingTypeDao;
	}

	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}

	public IOwnerDao getOwnerDao() {
		return ownerDao;
	}

	public void setOwnerDao(IOwnerDao ownerDao) {
		this.ownerDao = ownerDao;
	}

	public INewsDao getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(INewsDao newsDao) {
		this.newsDao = newsDao;
	}

	public IMessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(IMessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public IStrongNewsDao getStrongNewsDao() {
		return strongNewsDao;
	}

	public void setStrongNewsDao(IStrongNewsDao strongNewsDao) {
		this.strongNewsDao = strongNewsDao;
	}

	// role , user 对象事务处理
	@Override
	public void deleteRoleById(int id) {
		Role role = roleDao.findById(id);
		roleDao.delete(role);
	}

	@Override
	public void deleteUserById(int id) {
		User user = userDao.findById(id);
		userDao.delete(user);
	}

	@Override
	public Role findRoleById(int id) {
		return roleDao.findById(id);
	}

	@Override
	public Role findRoleByName(String name) {
		return roleDao.findRoleByName(name);
	}

	@Override
	public List<Role> findRoles() {
		return roleDao.findAll();
	}

	@Override
	public User findUserById(int id) {
		return userDao.findUserById(id);
	}

	@Override
	public User findUserByUsername(String username) {
		return userDao.findUserByUserName(username);
	}

	@Override
	public List<User> findUsers() {
		return userDao.findAll();
	}

	@Override
	public List<User> findUsersByRoleName(String roleName) {
		return roleDao.findUsersByRoleName(roleName);
	}

	@Override
	public void saveRole(Role role) {
		roleDao.save(role);
	}

	@Override
	public void saveUser(User user) {
		userDao.save(user);
	}

	@Override
	public void updateRole(Role role) {
		roleDao.update(role);
	}

	@Override
	public void updateUser(User user) {
		userDao.update(user);
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		User user = userDao.findUserByUserName(username);

		if (user == null) {
			throw new UsernameNotFoundException(messages.getMessage(
					"JdbcDaoImpl.notFound", new Object[] { username },
					"Username {0} not found"), username);
		}

		Set<Role> roles = user.getRoles();
		if (roles == null || roles.size() == 0) {
			throw new UsernameNotFoundException(messages.getMessage(
					"JdbcDaoImpl.notAuthority", new Object[] { username },
					"User {0} has no GrantedAuthority"), username);
		}

		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		for (Iterator<Role> iter = roles.iterator(); iter.hasNext();) {
			Role role = (Role) iter.next();
			GrantedAuthority authority = new GrantedAuthorityImpl("ROLE_"
					+ role.getName());
			authorities.add(authority);
		}

		return new org.springframework.security.core.userdetails.User(
				user.getUsername(), user.getPassword(), true, true, true, true,
				authorities);
	}

	// department部门对象处理事务处理
	@Override
	public void deleteDepartmentById(int id) {
		Department dept = deptDao.findById(id);
		deptDao.delete(dept);
	}

	@Override
	public Department findDepartmentById(int id) {
		return deptDao.findById(id);
	}

	@Override
	public Department findDepartmentByDeptId(String deptId) {
		return deptDao.findDepartmentByDeptId(deptId);
	}

	@Override
	public List<Department> findDepartments() {
		return deptDao.findAll();
	}

	@Override
	public void saveDepartment(Department department) {
		deptDao.save(department);
	}

	@Override
	public void updateDepartment(Department department) {
		deptDao.update(department);
	}

	// District部门对象处理事务处理
	@Override
	public void deleteDistrictById(int id) {
		District district = districtDao.findById(id);
		districtDao.delete(district);
	}

	@Override
	public District findDistrictById(int id) {
		return districtDao.findById(id);
	}

	@Override
	public District findDistrictByDistrictId(String districtId) {
		return districtDao.findDistrictByDistrictId(districtId);
	}

	@Override
	public List<District> findDistricts() {
		return districtDao.findAll();
	}

	@Override
	public void saveDistrict(District district) {
		districtDao.save(district);
	}

	@Override
	public void updateDistrict(District district) {
		districtDao.update(district);
	}

	// SubDistrict 对象处理事务
	@Override
	public void saveSubDistrict(SubDistrict subDistrict) {
		subDistrictDao.save(subDistrict);

	}

	@Override
	public void deleteSubDistrictById(int id) {
		SubDistrict subDistrict = subDistrictDao.findById(id);
		subDistrictDao.delete(subDistrict);
	}

	@Override
	public void updateSubDistrict(SubDistrict subDistrict) {
		subDistrictDao.update(subDistrict);

	}

	@Override
	public List<SubDistrict> findSubDistricts() {
		return subDistrictDao.findAll();
	}

	@Override
	public List<SubDistrict> findSubDistrictsOfDistrict(District district) {
		return subDistrictDao.findSubDistrictsOfDistrict(district.getId());
	}

	@Override
	public SubDistrict findSubDistrictById(int id) {
		return subDistrictDao.findById(id);
	}

	@Override
	public SubDistrict findSubDistrictBySubDistrictId(String subDistrictId) {
		return subDistrictDao.findSubDistrictsOfSubDistrictId(subDistrictId);
	}

	@Override
	public void deleteBuildingById(int id) {
		Building b = buildingDao.findById(id);
		buildingDao.delete(b);
	}

	@Override
	public void deleteHouseById(int id) {
		House h = houseDao.findById(id);
		houseDao.delete(h);
	}

	@Override
	public void deleteOwnerById(int id) {
		Owner o = ownerDao.findById(id);
		ownerDao.delete(o);
	}

	@Override
	public Building findBuildingByBuildingId(String buildingId) {
		return buildingDao.findByBuildingId(buildingId);
	}

	@Override
	public Building findBuildingById(int id) {
		return buildingDao.findById(id);
	}

	@Override
	public List<Building> findBuildings() {
		return buildingDao.findAll();
	}

	@Override
	public PageModel findBuildingList(int offset, int districtId) {
		return buildingDao.findBuildingList(offset, districtId);
	}

	@Override
	public House findHouseByHouseId(String houseId) {
		return houseDao.findByHouseId(houseId);
	}

	@Override
	public House findHouseById(int id) {
		return houseDao.findById(id);
	}

	@Override
	public List<House> findHouses() {
		return houseDao.findAll();
	}

	@Override
	public Owner findOwnerById(int id) {
		return ownerDao.findById(id);
	}

	@Override
	public Owner findOwnerByOwnerId(String ownerId) {
		return ownerDao.findByOwnerId(ownerId);
	}

	@Override
	public List<Owner> findOwners() {
		return ownerDao.findAll();
	}

	@Override
	public void saveBuilding(Building building) {
		buildingDao.save(building);
	}

	@Override
	public void saveHouse(House house) {
		houseDao.save(house);
	}
    
	/**
	 * 根据小区编号查找该小区的所有房产
	 */
	@Override
	public List<House> findHousesBySubId(SubDistrict subDistrict) {
		return houseDao.findBySubDistrict(subDistrict);
	}

	@Override
	public void saveOwner(Owner owner) {
		ownerDao.save(owner);
	}

	@Override
	public void updateBuilding(Building building) {
		buildingDao.update(building);
	}

	@Override
	public void updateHouse(House house) {
		houseDao.update(house);
	}

	@Override
	public void updateOwner(Owner owner) {
		ownerDao.update(owner);
	}

	@Override
	public PageModel findOwnerList(int offset) {
		return ownerDao.findOwnerList(offset);
	}

	@Override
	public Owner findOwnerByIdCard(String idCard) {
		return ownerDao.findOwnerByIdCard(idCard);
	}

	@Override
	public PageModel findHouseList(int offset, int districtId) {
		return houseDao.findHouseList(offset, districtId);
	}

	/*
	 * News对象处理事务处理
	 */
	@Override
	public List<News> findNewsListView(int belong) {
		return newsDao.findNewsListView(belong);
	}

	@Override
	public void saveNews(News news) {
		newsDao.save(news);
	}

	@Override
	public PageModel findNewsList(int offset, int belong) {
		return newsDao.findNewsByPage(offset, belong);
	}

	@Override
	public News findNewsById(int id) {
		return newsDao.findNewsById(id);
	}

	@Override
	public void updateNewsStateById(News news) {
		newsDao.update(news);
	}

	@Override
	public News findNewsByState() {
		return newsDao.findNewsByState();
	}

	@Override
	public void deleteNewsById(int id) {
		News news = newsDao.findNewsById(id);
		newsDao.delete(news);
	}

	@Override
	public void updateNews(News news) {
		newsDao.update(news);
	}

	@Override
	public News findNewsByBelong(int belong) {
		return newsDao.findNewsByBelong(belong);
	}

	/*
	 * StrongNews对象处理事务处理
	 */
	@Override
	public List<StrongNews> findStrongNewsListView(int belong) {
		return strongNewsDao.findStrongNewsListView(belong);
	}

	@Override
	public void saveStrongNews(StrongNews strongNews) {
		strongNewsDao.save(strongNews);
	}

	@Override
	public PageModel findStrongNewsList(int offset, int belong) {
		return strongNewsDao.findStrongNewsByPage(offset, belong);
	}

	@Override
	public StrongNews findStrongNewsById(int id) {
		return strongNewsDao.findStrongNewsById(id);
	}

	@Override
	public void updateStrongNewsStateById(StrongNews strongNews) {
		strongNewsDao.update(strongNews);
	}

	@Override
	public StrongNews findStrongNewsByState() {
		return strongNewsDao.findStrongNewsByState();
	}

	@Override
	public void deleteStrongNewsById(int id) {
		StrongNews StrongNews = strongNewsDao.findStrongNewsById(id);
		strongNewsDao.delete(StrongNews);
	}

	@Override
	public void updateStrongNews(StrongNews strongNews) {
		strongNewsDao.update(strongNews);
	}

	@Override
	public StrongNews findStrongNewsByBelong(int belong) {
		return strongNewsDao.findStrongNewsByBelong(belong);
	}
	
	
	// Message 处理
	@Override
	public void saveMessage(Message message) {
		messageDao.save(message);

	}

	@Override
	public Message findMessageById(int id) {
		return messageDao.findById(id);
	}

	@Override
	public void updateMessage(Message message) {
		messageDao.update(message);
	}

	@Override
	public void deleteMessage(Message message) {
		messageDao.delete(message);
	}

	@Override
	public List<Message> findMessageListForTen() {
		return messageDao.findMessageListForTen();
	}

	@Override
	public PageModel findMessageList(int offset) {
		return messageDao.findMessageList(offset);
	}

	// 楼栋类型业务逻辑处理
	@Override
	public void deleteBuildingTypeById(int id) {
		BuildingType type = buildingTypeDao.findById(id);
		buildingTypeDao.delete(type);
	}

	@Override
	public BuildingType findBuildingTypeById(int id) {
		return buildingTypeDao.findById(id);
	}

	@Override
	public List<BuildingType> findBuildingTypes() {
		return buildingTypeDao.findAll();
	}

	@Override
	public void saveBuildingType(BuildingType type) {
		buildingTypeDao.save(type);
	}

	@Override
	public void updateBuildingType(BuildingType type) {
		buildingTypeDao.update(type);
	}

	// 房产类型业务逻辑处理
	@Override
	public void deleteHouseTypeById(int id) {
		HouseType type = houseTypeDao.findById(id);
		houseTypeDao.delete(type);
	}

	@Override
	public HouseType findHouseTypeById(int id) {
		return houseTypeDao.findById(id);
	}

	@Override
	public List<HouseType> findHouseTypes() {
		return houseTypeDao.findAll();
	}

	@Override
	public void saveHouseType(HouseType houseType) {
		houseTypeDao.save(houseType);
	}

	@Override
	public void updateHouseType(HouseType houseType) {
		houseTypeDao.update(houseType);
	}

	// 房产来源业务逻辑处理
	@Override
	public void deleteHouseSourceById(int id) {
		HouseSource source = houseSourceDao.findById(id);
		houseSourceDao.delete(source);
	}

	@Override
	public HouseSource findHouseSourceById(int id) {
		return houseSourceDao.findById(id);
	}

	@Override
	public List<HouseSource> findHouseSources() {
		return houseSourceDao.findAll();
	}

	@Override
	public void saveHouseSource(HouseSource houseSource) {
		houseSourceDao.save(houseSource);
	}

	@Override
	public void updateHouseSource(HouseSource houseSource) {
		houseSourceDao.update(houseSource);
	}

	@Override
	public Owner findOwnerByName(String ownerName) {
		return ownerDao.findByName(ownerName);
	}

	@Override
	public Owner findOwnerByPhone(String phone) {
		return ownerDao.findByPhone(phone);
	}

	@Override
	public PageModel findOwnerListByDistrict(int offset, int districtId) {
		return ownerDao.findOwnerListByDistrict(offset,districtId);
	}

	@Override
	public PageModel findOwnerListBySubDistrict(int offset, int subDistrict_id) {
		return ownerDao.findOwnerListBySubDistrict(offset,subDistrict_id);
	}

	@Override
	public List<House> findAllHousesBySubDistrict(SubDistrict subDistrict) {
		return houseDao.findAllBySubDistrict(subDistrict);
	}

	@Override
	public List<Building> findBuildingsBySubDistrict(int id) {
		return buildingDao.findBuildingsBySubDistrict(id);
	}

	@Override
	public Owner findOwnerByHouseIdAndIdCard(String houseId, String idCard) {
		return ownerDao.findOwnerByHouseIdAndIdCard(houseId,idCard);
	}

	@Override
	public House findhouseByOwner(Owner owner) {
		return houseDao.findhouseByOwner(owner);
	}

	@Override
	public Owner findOwnerByHouseIdAndPassword(String houseId, String password) {
		return ownerDao.findOwnerByHouseIdAndPassword(houseId,password);
	}

}
